package com.geekaca.web.servlet;

import com.geekaca.web.mapper.UserMapper;
import com.geekaca.web.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;

@WebServlet(urlPatterns = "/register")
public class RegServlet extends HttpServlet {
    private static SqlSession openSession() {
        //1. 加载mybatis的核心配置文件,获取 SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream ips = null;
        try {
            ips = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(ips);
        //2. 获取SqlSession 对象, 用农来执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        return sqlSession;
    }


    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setHeader("Content-type", "text/html; charset=utf-8");

        String username = req.getParameter("username");
        String repassword = req.getParameter("repassword");

        String password = req.getParameter("password");
        String genderid = req.getParameter("gender");

        Integer gender = Integer.valueOf(genderid);
        String[] hobbies = req.getParameterValues("hobby");
        StringBuilder sb = new StringBuilder();
        String hobbiesSb;
        if (hobbies != null) {
            for (int i = 1; i <= hobbies.length; i++) {
                sb.append(i);
                if (i != hobbies.length) {
                    sb.append(",");
                }
            }
            hobbiesSb = sb.toString();
        } else {
            hobbiesSb = "";
        }

        String city = req.getParameter("city");
        String desc = req.getParameter("desc");

        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setHobbies(hobbiesSb);
        user.setGender(gender);
        user.setCity(city);
        user.setDesc(desc);
        PrintWriter writer = resp.getWriter();

        SqlSession sqlSession = openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        if (!username.trim().equals("")) {
            int cnt = userMapper.selectByUname(user);
            //用户名未使用
            if (cnt < 1) {
                if (!password.trim().equals("") && password.equals(repassword)) {
                    int cnt2 = userMapper.addUser(user);
                    if (cnt2 > 0) {
                        writer.write("<b>" + username + ": 注册成功！</b>");
                        String contextPath = req.getContextPath();
                        writer.write("<a href='" + contextPath + "/login.html'><b>点击登录</b></a>");
                    } else {
                        writer.write("注册失败！");
                    }
                } else {
                    writer.write("<b>两次密码输入不一致</b>");
                }
            //用户名已使用
            } else {
                writer.write("已存在该用户名，请选择其他用户名！");
            }
        }


    }
}
